package org.diysearch.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.diysearch.interfaces.Product;
import org.diysearch.interfaces.Service;
import org.diysearch.services.BaseExceptionService;
import org.diysearch.tools.BaseTool;

/**
 * Servlet implementation class for Servlet: controller
 *
 */
 public class Controller extends HttpServlet implements Servlet {
   static final long serialVersionUID = 1L;
   private static final Log LOG = LogFactory.getLog(Controller.class);
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Controller() {
		super();
	}   	
	
	private void process (HttpServletRequest request, 
			HttpServletResponse response) throws Exception
	{
		String action = request.getParameter("action");
		Service svc = null;
		LOG.debug("action: " + action);
		if ("/".equals(action) || action == null) {
			// default action
			svc = (Service) BaseTool.IOC_ENGINE.getBean("_default_");
		} else {
			action = action.replaceAll("/","");
			if (BaseTool.IOC_ENGINE.hasBean(action+"_AOP")) {
				svc = (Service) BaseTool.IOC_ENGINE.getBean(action+"_AOP");
			} else {
				svc = (Service) BaseTool.IOC_ENGINE.getBean(action);
			}
		}
		
		if (svc != null) {
			LOG.debug("Service: " + svc.getClass().getName());
			if (svc.isWriter()) {
				LOG.debug("setting writer");
				svc.setWriter(response.getWriter());
			} else {
				svc.setOutputStream(response.getOutputStream());
			}
			// set our req/resp objects into our service
			svc.setRequest(request);
			svc.setResponse(response);
		} else {
			LOG.error("service was null, this is not good");
			PrintWriter out = response.getWriter();
			out.println("service was null");
		}
		
		try {
			Product p = svc.execute();
			response.setContentType(p.getContentType());
			p.print();
		} catch (Exception ex) {
			BaseExceptionService errorSvc = (BaseExceptionService) 
				BaseTool.IOC_ENGINE.getBean("error-service");
			Product errorProd = errorSvc.execute(ex);
			errorProd.print();
		}
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException 
	{
		try {
			process(request,response);
		} catch (Exception ex) {
			
		}
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException 
	{
		try {
			process(request,response);
		} catch (Exception ex) {
			
		}
	}   	  	  
	
	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() throws ServletException {
		super.init();
	}   
}